HardwareTimer: Allow delaying initialization to setup method#1534
Merged
fpistm merged 1 commit intostm32duino:mainfrom Nov 10, 2021
Merged
HardwareTimer: Allow delaying initialization to setup method#1534fpistm merged 1 commit intostm32duino:mainfrom
fpistm merged 1 commit intostm32duino:mainfrom
Conversation
Contributor
Author
|
Note that I have not tested this particular commit in the main branch, but it is forward-ported verbatim from a slightly customized STM32 core we are using (where this commit works as expected). Looking at the diff between both versions for HardwareTimer, I would not expect any issues. |
Previously, the only way to initialize a HardwareTimer instance was to pass an instance to the constructor. When using a global (more specifically, with static storage duration) HardwareTimer instance, the initialization would happen early in startup, before main() and setup() had a chance to run. This would mean that on any errors (e.g. no timer found for a specific pin when using e.g. pinmap_peripheral), the board would just lock up in early startup, unable to show a meaningful error at all. To prevent this, you would have to allocate the HardwareTimer object dynamically on the heap, but that is not always a good idea from a memory management perspective. This commit adds an argumentless constructor that does not initialize the timer yet, and a setup() method that accepts the timer instance and does the actual initialization. This allows delaying the actual initialization until in or after the sketch setup() function, and also makes it easier to change the timer to use dynamically, based on e.g. user input or EEPROM contents or similar. Note that de-initializing a timer and switching to using a different one is not currently supported, trying to call setup() twice results in an error.
1116f9b to
b584df1
Compare
Contributor
Author
|
I've pushed some astyle fixes. |
Member
|
Hi @matthijskooijman |
ABOSTM
approved these changes
Nov 10, 2021
Contributor
ABOSTM
left a comment
There was a problem hiding this comment.
@matthijskooijman Thanks for this PR. It makes sense.
LGTM.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Previously, the only way to initialize a HardwareTimer instance was to
pass an instance to the constructor. When using a global (more
specifically, with static storage duration) HardwareTimer instance, the
initialization would happen early in startup, before main() and setup()
had a chance to run. This would mean that on any errors (e.g. no timer
found for a specific pin when using e.g. pinmap_peripheral), the board
would just lock up in early startup, unable to show a meaningful error
at all.
To prevent this, you would have to allocate the HardwareTimer object
dynamically on the heap, but that is not always a good idea from a
memory management perspective.
This commit adds an argumentless constructor that does not initialize
the timer yet, and a setup() method that accepts the timer instance and
does the actual initialization. This allows delaying the actual
initialization until in or after the sketch setup() function, and also
makes it easier to change the timer to use dynamically, based on e.g.
user input or EEPROM contents or similar.
Note that de-initializing a timer and switching to using a different one
is not currently supported, trying to call setup() twice results in an
error.